<template>
    <div id="poster">
    <el-form :model="loginForm" :rules="rules" ref="loginForm" class="login-container" label-position="left" label-width="0px">
        <h3 class="login_title">账号登录</h3>
        <el-form-item prop="username">
            <el-input type="text" v-model="loginForm.username" auto-complete="off" placeholder="账号" prefix-icon="el-icon-user-solid"></el-input>
        </el-form-item>

        <el-form-item prop="password">
            <el-input type="password" v-model="loginForm.password" auto-complete="off" placeholder="密码" prefix-icon="el-icon-lock"></el-input>
        </el-form-item>

        <el-form-item style="width: 100%">
            <el-button type="primary" style="width: 100%;background: #0FF;border: none" v-on:click="login('loginForm')">登录</el-button>
        </el-form-item>
        <el-form-item style="width: 100%">
            <router-link to="/register">
                <el-button type="primary" style="width: 100%;background: #90EE90;border: none" @click="goRegister">注册</el-button>
            </router-link>

        </el-form-item>
    </el-form>
    </div>
</template>


<script>
    export default {
        name: "Login",
        data() {
            return {
                loginForm: {
                    username: '',
                    password: ''
                },
                rules: {
                    username: [
                        { required: true, message: '请输入用户名', trigger: 'blur' },
                    ],
                    password: [
                        { required: true, message: '请输入密码', trigger: 'blur' },
                    ],
                }
            }
        },
        methods: {
            login(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        this.$axios.post("/user/login",{
                            uUsername:this.loginForm.username,
                            uPassword:this.loginForm.password,
                        }).then(ret=>{
                            console.log(ret)
                           if(ret.data==""){
                                this.$message.error('登陆失败');
                            }else{
                               sessionStorage.setItem('token',ret.data.token);
                               sessionStorage.setItem('user',JSON.stringify(ret.data.user));
                               /*管理角色退出又登录*/
                               if (ret.data.user.uUsername=='admin'){
                                   this.$message.success('登陆成功');
                                   this.$router.push('/BackHome')
                               }else {
                                   this.$message.success('登陆成功');
                                   let redirect=decodeURIComponent(this.$route.query.redirect)
                                   console.log(redirect)
                                   if(redirect=='undefined'){
                                       this.$router.push("/");
                                   }else{
                                       this.$router.push({path:redirect});
                                   }

                               }
                           }
                        });
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            },
            gotoBackHome(){
                this.$store.commit('controlBackHome',true);
                this.$router.push('/App')
            },
            goRegister:function () {
                this.$router.push('/register')
            },

        },

    }
</script>

<style scoped>
    #poster {
        background-color:#333;
        height: 100%;
        width: 100%;
        background-size: cover;
        position: fixed;
    }


    .login-container {
        border-radius: 15px;
        background-clip: padding-box;
        margin: 10vh auto;
        width: 350px;
        padding: 35px 35px 15px 35px;
        background: #fff;
        border: 1px solid #eaeaea;
        box-shadow: 0 0 25px #cac6c6;
    }

    .login_title {
        margin: 0px auto 40px auto;
        text-align: center;
        color: #505458;
    }


</style>
